package com.hyjx.framework.uc.subsys;

import com.hyjx.framework.common.CommonUtil;
import com.hyjx.framework.service.RightManager;
import com.hyjx.framework.service.UserCenterService;
import com.hyjx.framework.uc.menu.MenuModel;
import com.hyjx.framework.uc.menu.MenuService;
import com.hyjx.framework.uc.user.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 子系统 控制器
 */
@Controller
@RequestMapping("/subsys")
public class SubsysController {
    @Resource
    private SubsysService service;

    @Resource
    private MenuService menuService;

    /**
     * 跳转到管理首页
     *
     * @return
     */
    @RequestMapping(value = "/gotoList", method = {RequestMethod.GET, RequestMethod.POST})
    public String gotoSubsysList() {
        return "framework/uc/subsys/list";
    }

    /**
     * 查询列表
     *
     * @return
     */
    @RequestMapping(value = "/doList", produces = "application/json; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String querySubsysList(SubsysModel testModel) {
        UserInfo ui = UserCenterService.getCurrentUserInfo();

        return service.getSubsys(testModel);
    }

    /**
     * 跳转到增加页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoAdd", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoAdd(HttpServletRequest request) {
        SubsysModel model = new SubsysModel();
        model.setSubsys_id(CommonUtil.getUUID32());
        model.setValidity("Y");
        ModelAndView mav = new ModelAndView("framework/uc/subsys/add");
        mav.addObject("model", model);

        request.getSession().setAttribute("token", CommonUtil.getUUID32());
        return mav;
    }

    public boolean ValidateToken(HttpServletRequest request) {
        String token = request.getParameter("token");
        if (StringUtils.isEmpty(token))
            return true;
        String server_token = (String) request.getSession().getAttribute("token");
        if (StringUtils.isEmpty(server_token))
            return true;
        if (!StringUtils.equals(token, server_token))
            return true;
        return false;

    }

    /**
     * 保存新增的数据
     *
     * @param testModel
     * @return
     */
    @RequestMapping(value = "/doAdd", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String doAdd(HttpServletRequest request, SubsysModel testModel) {
//		if(ValidateToken(request))
//		{
//			System.out.println("重复提交");
//			return "notoken";
//		}
//		request.getSession().removeAttribute("token");
//		try {
//			Thread.sleep(3000);
//		} catch (InterruptedException e1) {
//			e1.printStackTrace();
//		}
        testModel.setSubsys_id(CommonUtil.getUUID32());
        String successFlag = "";
        try {
            List<SubsysModel> list = service.getAllSubsys(testModel);
            if (list == null || list.size() == 0) {
                if (service.add(testModel)) {
                    RightManager.init(2);
                    successFlag = "success";
                } else {
                    successFlag = "failure";
                }
            } else {
                successFlag = "repeat";
            }

        } catch (Exception e) {
            return "failure";
        }
        return successFlag;

    }

    /**
     * 跳转到编辑页面
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoEdit", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView gotoEdit(HttpServletRequest request) {
        String id = request.getParameter("id");
        SubsysModel model = new SubsysModel();

        model = service.getSubsysById(id);
        ModelAndView mav = new ModelAndView("framework/uc/subsys/edit");
        mav.addObject("model", model);
        return mav;
    }

    /**
     * 跳转到查看页面
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoView", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView gotoView(HttpServletRequest request) {
        String id = request.getParameter("id");
        SubsysModel model = new SubsysModel();
        request.setAttribute("showFlag", request.getParameter("showFlag"));
        model = service.getSubsysById(id);
        ModelAndView mav = new ModelAndView("framework/uc/subsys/view");
        mav.addObject("model", model);
        return mav;
    }

    /**
     * 更新
     *
     * @param testModel
     * @return
     */
    @RequestMapping(value = "/doUpdate", method = RequestMethod.POST)
    @ResponseBody
    public String doUpdate(SubsysModel testModel) {
        try {

            List<SubsysModel> list = service.getAllSubsys(testModel);
            if (list != null && list.size() > 0) {
                if (list.size() == 1) {
                    SubsysModel sm = list.get(0);
                    if (testModel.getSubsys_id().equals(sm.getSubsys_id())) {
                        if (service.update(testModel) > 0) {
                            RightManager.init(2);
                            return "success";
                        } else {
                            return "failure";
                        }
                    } else {
                        return "repeat";
                    }
                } else {
                    return "repeat";
                }
            } else {//没有找到匹配的（编码和名字相同的）
                if (service.update(testModel) > 0) {
                    RightManager.init(2);
                    return "success";
                } else {
                    return "failure";
                }
            }


        } catch (Exception e) {
            return "failure";
        }

//		return "failure";

    }

    /**
     * 删除
     * request
     *
     * @return
     */
    @RequestMapping(value = "/doDelete")
    @ResponseBody
    public String doDelete(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        MenuModel model = new MenuModel();
        model.setSubsys_id(ids);
        List<MenuModel> list = menuService.getMenuList(model);
        if (list == null || list.size() == 0) {
            if (service.delete(ids) > 0) {
                RightManager.init(2);
                return "success";
            } else {
                return "failure";
            }
        } else {
            return "exist";
        }

    }
}